package com.example.chattingroom.api;

import com.example.chattingroom.model.Friend;
import com.example.chattingroom.model.FriendMapper;
import com.example.chattingroom.model.User;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.ArrayList;

@RestController
public class FriendAPI {
    @Resource
    private FriendMapper friendMapper;

    @GetMapping("/friendList")
    @ResponseBody
    public Object getFriendList(HttpServletRequest req)
    {
        //先获取会话，从会话里获取userId
        HttpSession session = req.getSession(false);
        //如果会话不存在（没有登录），返回一个空列表
        if(session == null)
        {
            System.out.println("当前session不存在，请先登录！");
            return new ArrayList<Friend>();
        }
        User user = (User) session.getAttribute("user");

        //如果用户不存在，同样返回一个空列表
        if(user == null)
        {
            System.out.println("user不存在！");
            return new ArrayList<Friend>();
        }
        List<Friend> friendList = friendMapper.selectFriendList(user.getUserId());
        return  friendList;
    }
}
